using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.UI;
using System.Collections;

namespace NGroup.VTL.Core.Utilities
{
	public class Watermark 
	{

        public static bool CreateWatermark(string sourceFile,
            string logoImageFile = "/Content/images/logo.gif",
            int marginBottom = 5,
            int marginRight = 5,
            int transparency = 80)
		{
            
			//search watermark image
			
		    if (File.Exists(HttpContext.Current.Server.MapPath(logoImageFile)))
			    logoImageFile = HttpContext.Current.Server.MapPath(logoImageFile);
		    else
		    {
                return false;
		    }
            if (transparency > 100) transparency = 100;
            if (transparency < 0) transparency = 0;
            transparency = transparency * 255 / 100;

			//load bitmaps
			Image sourceImage = new Bitmap(sourceFile);
			Bitmap destinationBmp = new Bitmap(sourceImage.Width, sourceImage.Height);
			Bitmap logoImage = new Bitmap(logoImageFile);
			Graphics graphics = Graphics.FromImage(destinationBmp);

			//convert to indexes image
			Bitmap bitmapImage = CreateNonIndexedImage(logoImage);

			//set transparency
			for (int i = 0; i < logoImage.Width; i++)
				for (int j = 0; j < logoImage.Height; j++)
				{
					Color logoImagePixel = logoImage.GetPixel(i, j);
					if (logoImagePixel.A != 0)
						bitmapImage.SetPixel(i, j, Color.FromArgb(transparency, logoImagePixel));
				}

            //resize watermark - if is necessary
            if (logoImage.Width > sourceImage.Width || logoImage.Height > sourceImage.Height)
                bitmapImage = resizeImage(logoImage,
                    new Size(logoImage.Width > sourceImage.Width ? sourceImage.Width : logoImage.Width,
                        logoImage.Height > sourceImage.Height ? sourceImage.Height : logoImage.Height));
			graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			System.Drawing.Imaging.ImageFormat sourceImageFormat = sourceImage.RawFormat;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
			graphics.DrawImage(sourceImage,0,0,sourceImage.Width,sourceImage.Height);
            graphics.DrawImage(bitmapImage, marginRight, marginBottom, sourceImage.Width, sourceImage.Height);
			//Dispose objects
			sourceImage.Dispose();
			logoImage.Dispose();
			bitmapImage.Dispose();
			graphics.Dispose();
			destinationBmp.Save(sourceFile, sourceImageFormat);
			sourceImage.Dispose();
            return true;
		}

        public static Bitmap resizeImage(Image imgToResize, Size size)
		{
			int sourceWidth = imgToResize.Width;
			int sourceHeight = imgToResize.Height;
			float nPercent = 0;
			float nPercentW = nPercentW = ((float)size.Width / (float)sourceWidth);
			float nPercentH = nPercentH = ((float)size.Height / (float)sourceHeight);
			nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;

			int destWidth = (int)(sourceWidth * nPercent);
			int destHeight = (int)(sourceHeight * nPercent);

			Bitmap bitmap = new Bitmap(destWidth, destHeight);
			Graphics graphics = Graphics.FromImage((Image)bitmap);
			graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			graphics.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
			graphics.Dispose();
			return bitmap;
		}

        public static Bitmap CreateNonIndexedImage(Bitmap src)
		{
			Bitmap newBmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			using (Graphics gfx = Graphics.FromImage(newBmp))
				gfx.DrawImage(src, 0, 0);
			return newBmp;
		}
	}
}
